// pages/complaint/complaint.js
// pages/euqip/equip.js
const {
  API,
  defaultPage,
  equipStatusEnum,
  CLINIC_MANAGER_CODE
} = require('../../utils/const.js');
const {
  formVaildate,
  checkCount
} = require('../../utils/formCheckUtil.js');
const {
  getRequest,
  postRequest
} = require('../../utils/request.js');
const re = require('../../utils/request.js');
const {
  EuipFormRoles
} = require('../../utils/rulse.js');
const {
  hasRole
} = require('../../utils/userUtil.js');
var app = getApp();

Page({
  data: {
    patients: [],
    selectedPatient: '',
    selectedPatientName: '',
    agreementLetterBaseInfos: [],
    selectedAgreementLetterBaseInfo: '',
    selectedAgreementLetterBaseInfoName: '',
    equipments: [],
    selectedEquipments: []
  },

   /**
   * Lifecycle function--Called when page load
   */
  onLoad(options) {
    const that = this;
    // 发起请求，获取数据
    getRequest(API.getAgreementLetterBaseInfoList).then(res => {
      that.setData({
        agreementLetterBaseInfos: res
      });
    });

    getRequest(API.getMedicalEquipmentList).then(res => {
      that.setData({
        equipments: res
      });
    });

    getRequest(API.getPatientList).then(res => {
      that.setData({
        patients: res
      });
    });
  },

  onPatientChange: function(e) {
    const index = e.detail.value; // 获取选中的索引
    const selectedPatient = this.data.patients[index]; // 根据索引获取选中的患者对象

    // 设置选中的患者对象和名字
    this.setData({
      selectedPatient: selectedPatient,
      selectedPatientName: selectedPatient.patientName
    });
  },

  onAgreementLetterBaseInfoChange(e) {
    const index = e.detail.value;
    const selectedagreementLetterBaseInfo = this.data.agreementLetterBaseInfos[index];

    this.setData({
      selectedAgreementLetterBaseInfo: selectedagreementLetterBaseInfo,
      selectedAgreementLetterBaseInfoName: selectedagreementLetterBaseInfo.letterName
    });
  },

  onEquipmentChange(e) {
    this.setData({
      selectedEquipments: e.detail.value
    });
  },

  onSubmit() {
    if (this.data.selectedPatient && this.data.selectedAgreementLetterBaseInfo) {
      // 这里可以添加实际提交的代码逻辑
      const user = wx.getStorageSync('userInfo');
      const doctorId = user.userId;
      const patientId = this.data.selectedPatient.patientId;
      const letterId = this.data.selectedAgreementLetterBaseInfo.letterId;
      const letterName = this.data.selectedAgreementLetterBaseInfo.letterName;
      const equipmentsData = JSON.stringify(this.data.selectedEquipments);
      var formValue = {
        'letterId': letterId,
        'letterName': letterName,
        'doctorId': doctorId,
        'patientId': patientId,
        'sendTime': new Date().toISOString(),
        'medicalEquipments': equipmentsData
      };
      postRequest(API.sendAgreementLetter, formValue).then(res => {
        console.log('send agreement letter successfully');
        if (res) {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000,
            complete: function() {
              // 延时跳转，确保提示显示完毕
              setTimeout(() => {
                wx.switchTab({
                  url: '/pages/home/home'
                });
              }, 2000);  // 2秒钟后跳转
            }
          });
        }
      });
    }
  }
});